<?php
namespace Admin\Model;
use Think\Model\RelationModel;
class UserAddressModel extends RelationModel
{
	protected $insertFields = array('consignee','province','city','district','address','mobile','is_default');
	protected $updateFields = array('id','member_id','consignee','province','city','district','address','mobile','is_default');
	protected $_validate = array(
		array('consignee', 'require', '收货人名字不能为空！', 1, 'regex', 3),
		array('consignee', '1,60', '收货人名字的值最长不能超过 60 个字符！', 1, 'length', 3),
		array('province', 'require', '省份不能为空！', 1, 'regex', 3),
		array('city', 'require', '城市不能为空！', 1, 'regex', 3),
		array('district', 'require', '地区不能为空！', 1, 'regex', 3),
		array('address', 'require', '详细地址不能为空！', 1, 'regex', 3),
		array('address', '1,120', '详细地址的值最长不能超过 120 个字符！', 1, 'length', 3),
		array('mobile', 'require', '手机号不能为空！', 1, 'regex', 3),
		array('mobile', '1,60', '手机号的值最长不能超过 60 个字符！', 1, 'length', 3),
	);
    protected $_link =array(
        array(
            'class_name'=>'Region',
            'mapping_type'=>self::HAS_ONE,
            'mapping_key' =>'province',
            'foreign_key' => 'region_id',
            'mapping_name'=>'province'
        ),array(
            'class_name'=>'Region',
            'mapping_type'=>self::HAS_ONE,
            'mapping_key' =>'city',
            'foreign_key' => 'region_id',
            'mapping_name'=>'city'
        ),array(
            'class_name'=>'Region',
            'mapping_type'=>self::HAS_ONE,
            'mapping_key' =>'district',
            'foreign_key' => 'region_id',
            'mapping_name'=>'district'
        ),
    );
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($member_id = session('m_id'))
			$where['member_id'] = array('eq', $member_id);
		if($consignee = I('get.consignee'))
			$where['consignee'] = array('like', "%$consignee%");
		if($province = I('get.province'))
			$where['province'] = array('eq', $province);
		if($city = I('get.city'))
			$where['city'] = array('eq', $city);
		if($district = I('get.district'))
			$where['district'] = array('eq', $district);
		if($address = I('get.address'))
			$where['address'] = array('like', "%$address%");
		if($zipcode = I('get.zipcode'))
			$where['zipcode'] = array('like', "%$zipcode%");
		if($mobile = I('get.mobile'))
			$where['mobile'] = array('like', "%$mobile%");
		if($addtime = I('get.addtime'))
			$where['addtime'] = array('like', "%$addtime%");
		if($is_default = I('get.is_default'))
			$where['is_default'] = array('eq', $is_default);
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this
            ->relation(true)
            ->alias('a')
            ->where($where)
            ->group('a.id')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
	    $data['is_default'] = 0;
	    $data['member_id'] = session('m_id');
        $data['addtime'] = time();
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}